package com.ashen.boot.admin.project.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ashen.boot.admin.project.system.user.model.dto.UserDataTypeDTO;
import com.ashen.boot.admin.project.system.user.model.entity.UserDataTypeEntity;
import com.ashen.boot.admin.project.system.user.mapper.IUserDataTypeMapper;
import com.ashen.boot.admin.project.system.user.model.query.UserDataTypeParams;
import com.ashen.boot.admin.project.system.user.service.IUserDataTypeService;
import com.ashen.boot.admin.domain.service.impl.SuperBaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户数据权限类型 服务实现类
 * </p>
 *
 * @author gao
 * @since 2021-11-08
 */
@Service
@RequiredArgsConstructor
public class UserDataTypeServiceImpl extends SuperBaseServiceImpl<Long, UserDataTypeParams, UserDataTypeDTO, UserDataTypeEntity, IUserDataTypeMapper> implements IUserDataTypeService {

    private final IUserDataTypeMapper mapper;

    @Override
    public boolean updateUserDataType(@Nonnull Long userId, @Nonnull Long dataPermissionId, @Nonnull int isall) {
        Assert.notNull(userId, "用户id为空");
        saveBatch(userId, dataPermissionId, isall);
        return true;
    }


    private void saveBatch(@Nonnull Long userId, @Nonnull Long dataPermissionId, @Nonnull int isall) {
        Assert.notNull(userId, "用户id为空");
        Assert.notNull(dataPermissionId, "权限id为空");
        //List<UserDataTypeEntity> entities = new ArrayList<>(1);
        UserDataTypeEntity entity = new UserDataTypeEntity();
        entity.setUserId(userId);
        entity.setDataPermissionId(dataPermissionId);
        entity.setIsall(isall);
        //entities.add(entity);
        LambdaQueryWrapper<UserDataTypeEntity> queryWrapper = Wrappers.lambdaQuery(UserDataTypeEntity.class).eq(UserDataTypeEntity::getUserId, userId).eq(UserDataTypeEntity::getDataPermissionId, dataPermissionId);
        saveOrUpdate(entity,queryWrapper);
        //saveBatch(entities);
    }
}
